package com.pz.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq.listener.simple.retry", name = "enabled",havingValue = "true")
public class MQErrorConfig {
    private static final String errorExchange = "error.exchange";
    private static final String errorRoutingKey = "error";
    private static final String errorQue = "error.que";

    /**
     *  接收错误消息的交换机
     */
    @Bean
    public DirectExchange errorExchange(){
        return new DirectExchange(errorExchange);
    }

    @Bean
    public Queue errorQue(){
        return new Queue(errorQue);
    }

    @Bean
    public Binding errorBinding(DirectExchange errorExchange, Queue errorQue){
        return BindingBuilder.bind(errorQue).to(errorExchange).with(errorRoutingKey);
    }

    @Bean
    public MessageRecoverer republishMsgRecovered(RabbitTemplate rabbitTemplate){
        return new RepublishMessageRecoverer(rabbitTemplate, errorExchange, errorRoutingKey);
    }
}
